import 'package:dio/dio.dart';
import 'package:hotpot/http/http.dart';
import 'package:hotpot/http/response_bean.dart';
import 'package:hotpot/odoo/odoo_helper.dart';
import 'package:hotpot/pages/table/table_page.dart';

abstract class HttpApi {
  /// 获取数据库
  static Future<dynamic> database() async {
    var result = await Dio(BaseOptions(baseUrl: OdooHelper.baseurl)).get('web/database/getdb');
    return result.data;
    return HttpManager().get('web/database/getdb', <String, dynamic>{});
  }

  /// 加载自助点餐机数据
  static Future<ResponseData> loadKioskData() {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.kiosk.interface/load_kiosk_data', {
      "model": "intpv.mobile.kiosk.interface",
      "method": "load_kiosk_data",
      "args": [],
      "kwargs": {"context": OdooHelper.context}
    });
  }

  /// 销售点查询
  static Future<ResponseData> getPosConfigList() {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.waiter.interface/get_pos_config_list', {
      "model": "intpv.mobile.waiter.interface",
      "method": "get_pos_config_list",
      "args": [],
      "kwargs": {"context": OdooHelper.context}
    });
  }

  /// 加载销售点
  static Future<ResponseData> loadPosConfigData(int configId) {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.waiter.interface/load_pos_config_data', {
      "model": "intpv.mobile.waiter.interface",
      "method": "load_pos_config_data",
      "args": [],
      "kwargs": {"context": OdooHelper.context, 'config_id': configId}
    });
  }

  /// 获取所有桌子的订单数与未发送厨房的商品数
  static Future<ResponseData> getTablesOrderCountChanges(int configId) {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.waiter.interface/get_tables_order_count_changes', {
      "model": "intpv.mobile.waiter.interface",
      "method": "get_tables_order_count_changes",
      "args": [],
      "kwargs": {"context": OdooHelper.context, 'config_id': configId}
    });
  }

  /// 根据会话和餐桌获取所有订单
  static Future<ResponseData> getSessionOrdersByTable(int sessionId, int tableId) {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.waiter.interface/get_session_orders_by_table', {
      "model": "intpv.mobile.waiter.interface",
      "method": "get_session_orders_by_table",
      "args": [],
      "kwargs": {"context": OdooHelper.context, 'session_id': sessionId, 'table_id': tableId}
    });
  }

  /// 订单保存
  static Future<ResponseData> createOrder(int sessionId, int tableId, int cashierId, int loginNumber, dynamic order) {
    return HttpManager().post('web/dataset/call_kw/intpv.mobile.waiter.interface/create_order', {
      "model": "intpv.mobile.waiter.interface",
      "method": "create_order",
      "args": [],
      "kwargs": {
        "context": OdooHelper.context,
        'session_id': sessionId,
        'table_id': tableId,
        'cashier_id': cashierId,
        'login_number': loginNumber,
        // if (customerCount != null) 'customer_count': customerCount,
        // if (partnerId != null) 'partner_id': partnerId,
        'order': order,
      }
    }).whenComplete(() => tableRefresh?.call());
  }

  /// 发送餐厅
  static Future<ResponseData> sendOrderToPreparation(int orderId) {
    return HttpManager().post('intpv_restaurant/send_order_to_preparation', {
      // "params": {
      //   'order_id': orderId,
      // }
      'order_id': orderId,
    });
  }

  /// 检测更新
  static Future<Map<String, dynamic>> checkUpdate() {
    return Dio().post('https://api.pgyer.com/apiv2/app/getByShortcut', queryParameters: {
      "_api_key": "64800c5a81ef22361f3d00cad87c6a0c",
      "buildShortcutUrl": "USkGAOx5",
    }).then((e) {
      return e.data;
    });
  }
}
